#include "translator.h"
#include "websocketclientwrapper.h"
#include "websockettransport.h"

#include <QWebChannel>
#include <QWebSocketServer>
#include <QDebug>

Translator::Translator(QObject *parent) : QObject(parent)
{
    QWebSocketServer* server = new QWebSocketServer(QStringLiteral("QWebChannel Highcharts comm example"), QWebSocketServer::NonSecureMode, this);
    if (!server->listen(QHostAddress::LocalHost, 12345)) {
        qFatal("Failed to open web socket server.");
    }

    // wrap WebSocket clients in QWebChannelAbstractTransport objects
    WebSocketClientWrapper* clientWrapper = new WebSocketClientWrapper(server, this);

    // setup the channel
    QWebChannel* channel = new QWebChannel(this);
    QObject::connect(clientWrapper, &WebSocketClientWrapper::clientConnected,
                     channel, &QWebChannel::connectTo);

    channel->registerObject(QStringLiteral("translator"), this);
}

void Translator::setValue(int value)
{
    emit receiveJsValue(value);
}
